<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>invertKeyValues</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const invertKeyValues = (obj, handler) =>
            Object.keys(obj).reduce((acc, key) => {
                const val = handler ? handler(obj[key]) : obj[key];
                acc[val] = acc[val] || [];
                acc[val].push(key);
                return acc;
            }, {});
        console.log(invertKeyValues({ a: 1, b: 2, c: 1 }));
    </script>
</body>

</html>